View Javadoc
1 /*** 2 * Registry 3 * 4 * This is a superclass of all types of 5 * registries out there. 6 * 7 * I thought it would be useful, since I've 8 * found myself writing the code for this 9 * many times. 10 * 11 * We should also be able to tie event driven 12 * stuff to it, as well as pull some things 13 * like a JTree node from it. 14 */ 15 16 package junit.quilt.util; 17 18 import javax.swing.tree.*; 19 import java.util.*; 20 21 public class Registry 22 extends Object 23 { 24 private String name; 25 private boolean rootVisible; 26 private DefaultMutableTreeNode root; 27 private Map registry = new HashMap(); // LEAF X CONTENTS 28 29 private List listeners = new ArrayList(); // RegistryListener 30 31 public Registry() { 32 name = "Registry"; 33 root = new DefaultMutableTreeNode( name ); 34 } 35 36 public Registry( String name ) { 37 this.name = name; 38 rootVisible = true; 39 root = new DefaultMutableTreeNode( name ); 40 } 41 42 public Object get( TreePath path ) { 43 Object pathElements[] = path.getPath(); 44 DefaultMutableTreeNode curr = root; 45 DefaultMutableTreeNode last = null; 46 47 if (!root.equals( pathElements[0] )) { return null; } 48 49 for (int i = 0; i < pathElements.length; i++) { 50 last = curr; 51 Enumeration children = curr.children(); 52 while (children.hasMoreElements()) { 53 DefaultMutableTreeNode child 54 = (DefaultMutableTreeNode) children.nextElement(); 55 if (child.equals( pathElements[i] )) { 56 curr = child; 57 } 58 } 59 } 60 61 if (last == curr) { 62 return null; 63 } 64 65 return registry.get(curr); 66 } 67 68 private DefaultMutableTreeNode findNode( Object keys[], boolean make ) { 69 DefaultMutableTreeNode curr = root; 70 DefaultMutableTreeNode last = null; 71 72 for (int i = 0; i < keys.length; i++) { 73 Enumeration children = curr.children(); 74 last = curr; 75 while (children.hasMoreElements()) { 76 DefaultMutableTreeNode child 77 = (DefaultMutableTreeNode) children.nextElement(); 78 79 if (child.getUserObject().equals( keys[i] )) { 80 curr = child; 81 } 82 } 83 84 if (last == curr) { // We did not find it. 85 if (make) { // We add. . . 86 DefaultMutableTreeNode next 87 = new DefaultMutableTreeNode( keys[i] ); 88 curr.add( next ); 89 curr = next; 90 } else { 91 return null; 92 } 93 } 94 } 95 return curr; 96 } 97 98 /*** 99 * Returns TRUE if it successfully adds it, FALSE 100 * if it already exists. 101 */ 102 103 protected boolean register( Object keys[], Object value ) { 104 DefaultMutableTreeNode curr = findNode( keys, true ); 105 106 // curr is the node it is stored under. 107 if (registry.containsKey( curr )) { return false; } // Already exists. 108 registry.put( curr, value ); 109 110 Iterator ls = listeners.iterator(); 111 while (ls.hasNext()) { 112 ((RegistryListener) ls.next()).newRegistration( keys, value ); 113 } 114 return true; 115 } 116 117 /*** 118 * Use this to register a listener here. It will let you 119 * know when the contents of the registry change. 120 */ 121 122 public void addRegistryListener( RegistryListener rl ) { 123 listeners.add( rl ); 124 } 125 126 public void removeRegistryListener( RegistryListener rl ) { 127 listeners.remove( rl ); 128 } 129 130 /*** 131 * Use this to get the contents of the registry, in a form 132 * that a JTree can use. 133 */ 134 135 public TreeNode getContents() { 136 return root; 137 } 138 139 public Object get( Object path[] ) { 140 DefaultMutableTreeNode node = findNode( path, false ); 141 142 if ((node != null) && (registry.containsKey( node ))) { 143 return registry.get( node ); 144 } 145 146 return null; 147 } 148 149 /*** 150 * Returns all entries in the Registry. 151 */ 152 153 public Iterator getValues() { 154 return registry.values().iterator(); 155 } 156 157 } 158

This page was automatically generated by Maven